package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.TblisustampEmailErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.vo.tblisustampemail.TblisustampEmailCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.tblisustampemail.TblisustampEmailPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.tblisustampemail.TblisustampEmailUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;


import java.util.*;

import com.cmc.cloud.cmclink.doc.service.TblisustampEmailService;
import com.cmc.cloud.cmclink.doc.entity.TblisustampEmailDO;
import com.cmc.cloud.cmclink.doc.mapper.TblisustampEmailMapper;


/**
 * 不同代理的申请记录确认人和确认人邮箱配置 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class TblisustampEmailServiceImpl implements TblisustampEmailService {

    @Resource
    private TblisustampEmailMapper tblisustampEmailMapper;

    @Override
    public void createTblisustampEmail(TblisustampEmailCreateReqVO createReqVO) {
        // 插入
        TblisustampEmailDO tblisustampEmail = BeanUtil.copyProperties(createReqVO, TblisustampEmailDO.class);
        tblisustampEmailMapper.insert(tblisustampEmail);
    }

    @Override
    public void updateTblisustampEmail(TblisustampEmailUpdateReqVO updateReqVO) {
        // 校验存在
        validateTblisustampEmailExists(updateReqVO.getId());
        // 更新
        TblisustampEmailDO updateObj = BeanUtil.copyProperties(updateReqVO, TblisustampEmailDO.class);
        tblisustampEmailMapper.updateById(updateObj);
    }

    @Override
    public void deleteTblisustampEmail(Long id) {
        // 校验存在
        validateTblisustampEmailExists(id);
        // 删除
        tblisustampEmailMapper.deleteById(id);
    }

    private void validateTblisustampEmailExists(Long id) {
        if (tblisustampEmailMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(TblisustampEmailErrorCodeConstants.TBLISUSTAMP_EMAIL_NOT_EXISTS);
        }
    }

    @Override
    public TblisustampEmailDO getTblisustampEmail(Long id) {
        return tblisustampEmailMapper.selectById(id);
    }

    @Override
    public List<TblisustampEmailDO> getTblisustampEmailList(Collection<Long> ids) {
        return tblisustampEmailMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<TblisustampEmailDO> getTblisustampEmailPage(TblisustampEmailPageReqVO reqVO) {
        LambdaQueryWrapperX<TblisustampEmailDO> queryWrapperX = new LambdaQueryWrapperX<TblisustampEmailDO>().likeIfPresent(TblisustampEmailDO::getAgentCode, reqVO.getAgentCode());
        queryWrapperX.eqIfPresent(TblisustampEmailDO::getPolCode, reqVO.getPolCode());
        return tblisustampEmailMapper.selectPage(reqVO, queryWrapperX);
    }
}
